package com.xunlai.infra.permission.manager.sercurity;

import com.xunlai.infra.permission.common.util.SM2Utils;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @author liang
 * @date 12/7/22 5:39 PM
 */
public class SM2PasswordEncoder implements PasswordEncoder {
    private final PrivateKey privateKey;

    private final PublicKey publicKey;

    public SM2PasswordEncoder(PrivateKey privateKey, PublicKey publicKey) {
        this.privateKey = privateKey;
        this.publicKey = publicKey;
    }

    @Override
    public String encode(CharSequence rawPassword) {
        return SM2Utils.encrypt(privateKey,rawPassword.toString());
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        return SM2Utils.verify(publicKey,encodedPassword,rawPassword.toString());
    }
}
